import { dateFilter } from "./filter";
/**
 * 获取开始结束时间参数
 * @param {number,string} id =[number,string]
 */
export const tagChange = (id) => {
    let date = new Date();
    let params = {
        endTime: dateFilter(date),
    };
    if (typeof id === "number") {
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let d = date.getDate();
        if (d - id < 1) {
            m -= 1;
            let dNum = new Date(y, m, 0).getDate();
            d = dNum - Math.abs(d - id);
            if (m < 1) {
                y -= 1;
                m = 12;
            }
        } else {
            d -= id;
        }
        params.startTime = dateFilter(new Date(`${y}-${m}-${d}`));
    } else {
        let Month = date.getMonth();
        let year = date.getFullYear();
        switch (id) {
            case "本月":
                params.startTime = `${year}-${Month + 1}-1`;
                break;
            case "上月":
                if (Month == 0) {
                    year -= 1;
                    Month = 1;
                }
                let a = {
                    startTime: dateFilter(new Date(`${year}-${Month}-${1}`)),
                    endTime: dateFilter(new Date(`${year}-${Month}-${new Date(year, Month + 1, 0).getDate()}`)),
                };
                return a;
                break;
            case "本季":
                let jdArr = [1, 4, 7, 10];
                let jd = Math.trunc((Month + 1) / 3);
                params.startTime = dateFilter(new Date(`${year}-${jdArr[jd]}-1`));

                break;
            case "本年":
                params.startTime = dateFilter(new Date(`${year}-1-1`));
                break;
            default:
                break;
        }
    }
    return params;
};
/**
 * 姓名截取
 * @param {string} name 姓名
 */
export function compellationCut(name) {
    // var reg = /(?<=.)./g;
    // return name.replace(reg, '*');
    return "*" + name.substring(1, name.length);
}
/**
 * 身份证号截取
 * @param {string} card 身份证号
 */
export function idCardCut(card) {
    return card.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
}
/**
 * 手机号中间截取使用*号代替
 * @param {string} phone 手机号
 */
export function phoneCut(phone) {
    return phone.substr(0, 3) + "****" + phone.substr(7);
}
/**
 * 取消触底执行函数的监听
 */
export function offObserve() {
    observer.disconnect();
}

let timeout = null;

/**
 * 防抖原理：一定时间内，只有最后一次操作，再过wait毫秒后才执行函数
 *
 * @param {Function} func 要执行的回调函数
 * @param {Number} wait 延时的时间
 * @param {Boolean} immediate 是否立即执行
 * @return null
 */
export function debounce(func, wait = 500, immediate = true) {
    // 清除定时器
    if (timeout !== null) clearTimeout(timeout);
    // 立即执行，此类情况一般用不到
    if (immediate) {
        const callNow = !timeout;
        timeout = setTimeout(() => {
            timeout = null;
        }, wait);
        if (callNow) typeof func === "function" && func();
    } else {
        // 设置定时器，当最后一次操作后，timeout不会再被清除，所以在延时wait毫秒后执行func回调方法
        timeout = setTimeout(() => {
            typeof func === "function" && func();
        }, wait);
    }
}
